﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using Total_Mayhem.Common;
using System.Diagnostics;

namespace Total_Mayhem.Power
{
    /// <summary>
    /// Interaction logic for NetworkConfig.xaml
    /// </summary>
    public partial class MonitorPowerReactionsConfig : MayhemWpf.UserControls.WpfConfiguration
    {

        public MonitorPowerReactionsConfig()
        {
            this.strTitle = "Monitor Power Settings";
            InitializeComponent();
        }

        public override void OnLoad()
        {
            hlHelp.RequestNavigate += new RequestNavigateEventHandler(Network.Hyperlink_HandleClick);
            if (string.IsNullOrEmpty(NirCmd))
            {
                cmbCommand_SelectionChanged(null, null); //Init NirCmd property to default selected item
            }
            else
            {
                var saved = cmbCommand.Items.Cast<ComboBoxItem>().FirstOrDefault(i => i.Tag.ToString() == NirCmd);
                if (saved != null)
                {
                    saved.IsSelected = true; //Restore selection
                }
                else
                {
                    cmbCommand_SelectionChanged(null, null); //Saved value does not match, reset to default
                }
            }
            CanSave = true;
            //Set wpf element text here
            /*
            textBoxName.Text = ComputerName;
            if (string.IsNullOrEmpty(textBoxName.Text)) { textBoxName.Text = "android-bc685d7e8081788a"; } //TODO: Remove before release
            var saved = cmbInterval.Items.Cast<ComboBoxItem>().FirstOrDefault(i => i.Tag.ToString() == IntervalinSeconds.ToString());
            if (saved != null)
            {
                saved.IsSelected = true;
            }*/
        }

        private string strTitle;
        public override string Title { get {return strTitle;} }

        public string NirCmd
        {
            get;
            set;
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            textInvalid.Text = string.Format("Executing command: {0}",NirCmd);
            textInvalid.Visibility = System.Windows.Visibility.Visible;

            string strArgs = NirCmd.Replace("nircmd.exe ", "");
            Misc.ExecuteNirCmd(strArgs);
        }

        private void cmbCommand_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbCommand.SelectedItem != null)
            {
                ComboBoxItem i = (cmbCommand.SelectedItem as ComboBoxItem);
                if (i != null)
                {
                    NirCmd = (string)i.Tag;
                }
            }
        }

        /*
        public override void OnLoad()
        {
            //Set wpf element text here
            textBoxName.Text = ComputerName;
            if (string.IsNullOrEmpty(textBoxName.Text)) { textBoxName.Text = "android-bc685d7e8081788a"; } //TODO: Remove before release
            var saved = cmbInterval.Items.Cast<ComboBoxItem>().FirstOrDefault(i => i.Tag.ToString() == IntervalinSeconds.ToString());
            if (saved != null)
            {
                saved.IsSelected = true;
            }
            //var saved2 = cmbWait.Items.Cast<ComboBoxItem>().FirstOrDefault(i => i.Tag.ToString() == WaitinMilliSeconds.ToString());
            //if (saved2 != null)
            //{
            //    saved2.IsSelected = true;
            //}
        }

        private string strTitle;
        public override string Title
        {
            get
            {
                return strTitle;
            }
        }

        public string ComputerName { get; private set; }
        public int IntervalinSeconds { get; private set; }
        //public int WaitinMilliSeconds { get; private set; }


        private void Name_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckValidity();
            ComputerName = textBoxName.Text;
        }

        private void CheckValidity()
        {
            CanSave = true;
            if (Network.RegexIP.IsMatch(textBoxName.Text) || Network.RegexName.IsMatch(textBoxName.Text))
            {
                btnTest.IsEnabled = true;
            }
            else
            {
                textInvalid.Text = "Invalid Computer Name or IP Address";
                CanSave = false;
                btnTest.IsEnabled = true;
            }

            textInvalid.Visibility = CanSave ? Visibility.Collapsed : Visibility.Visible;
        }

        private void CheckSubdirs_Checked(object sender, RoutedEventArgs e)
        {

        }

        

        private void cmbWait_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbWait.SelectedItem != null)
            {
                ComboBoxItem i = (cmbWait.SelectedItem as ComboBoxItem);
                if (i != null && !string.IsNullOrEmpty((string)i.Tag))
                {
                    //WaitinMilliSeconds = int.Parse(i.Tag.ToString());
                }
            }
        }
         */
    }
}
